$(function () {
    $("#jqGrid").jqGrid({
        url: '../empcontract/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden: true},
			{ label: '合同编号', name: 'conId', index: 'con_id', width: 80 }, 			
			{ label: '员工编号', name: 'empNo', index: 'emp_no', width: 80 }, 			
			{ label: '员工姓名', name: 'empName', index: 'emp_name', width: 80 }, 			
			{ label: '员工身份证号', name: 'empCardno', index: 'emp_cardno', width: 80 }, 			
			{ label: '合同起始时间', name: 'conStartTime', index: 'con_start_time', width: 80 }, 			
			{ label: '合同终止时间', name: 'conEndTime', index: 'con_end_time', width: 80 }, 			
			{ label: '合同期限', name: 'conDeadline', index: 'con_deadline', width: 80 }, 			
			{ label: '试用期起始时间', name: 'tryStartTime', index: 'try_start_time', width: 80 }, 			
			{ label: '试用期终止时间', name: 'tryEndTime', index: 'try_end_time', width: 80 }, 			
			{ label: '试用期期限', name: 'tryDeadline', index: 'try_deadline', width: 80 }, 			
			{ label: '工作单位', name: 'jobUnit', index: 'job_unit', width: 80 }, 			
			{ label: '工作地点', name: 'jobPosition', index: 'job_position', width: 80 }, 			
			{ label: '工作制', name: 'jobInstitution', index: 'job_institution', width: 80 }, 			
			{ label: '合同方式', name: 'conType', index: 'con_type', width: 80 }, 			
			{ label: '录入人', name: 'operatePerson', index: 'operate_person', width: 80 }, 			
			{ label: '是否生效', name: 'ifEffective', index: 'if_effective', width: 80 }, 			
			{ label: '是否续签', name: 'isContinue', index: 'is_continue', width: 80 }, 			
			{ label: '工资发放日', name: 'payGiveDate', index: 'pay_give_date', width: 80 }, 			
			{ label: '试用期工资', name: 'tryPeriodPay', index: 'try_period_pay', width: 80 }, 			
			{ label: '转正计算方式', name: 'formalComputeType', index: 'formal_compute_type', width: 80 }, 			
			{ label: '月工资', name: 'monthPay', index: 'month_pay', width: 80 }, 			
			{ label: '社保', name: 'societySecure', index: 'society_secure', width: 80 }, 			
			{ label: '解除或终止日期', name: 'relieveOrEndDate', index: 'relieve_or_end_date', width: 80 }, 			
			{ label: '解除或终止原因', name: 'relieveOrEndReason', index: 'relieve_or_end_reason', width: 80 }, 			
			{ label: '有无经济补偿', name: 'ifEconomicCompensate', index: 'if_economic_compensate', width: 80 },
            { label: '一审日期', name: 'auditdate', index: 'auditdate', width: 80 },
            { label: '一审人', name: 'auditperson', index: 'auditperson', width: 60 },
            { label: '二审日期', name: 'fstime', index: 'fstime', width: 80 },
            { label: '二审人', name: 'fsr', index: 'fsr', width: 60 },
            { label: '审批状态', name: 'audit', index: 'audit', width: 60,formatter:getaudit,unformat:ungetaudit }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        //字段太多自动加载横轴滚动条
        shrinkToFit:false,
        autoScroll: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            name: null,

        },
		showList: true,
		title: null,
		empContract: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.empContract = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            if(rowData.audit!='0'){
                alert("已走审批的数据不允许修改！");
                return;
            }
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.empContract.id == null ? "../empcontract/save" : "../empcontract/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.empContract),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../empcontract/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../empcontract/info/"+id, function(r){
                vm.empContract = r.empContract;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'emp_name': vm.q.name},
                page:page
            }).trigger("reloadGrid");
		},
        firstaudit:function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            if(rowData.audit!='0'){
                alert("该条记录已在审批中或者还未到达一审节点！");
                return;
            }
            utils.openAuditView("../empcontract/simpleaudit",1,id,$("#jqGrid"));
        },
        secondaudit:function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            if(rowData.audit=='1'){
                utils.openAuditView("../empcontract/simpleaudit",2,id,$("#jqGrid"));
            }else{
                alert("该条记录已在审批中或者还未到二审节点！");
                return;
            }
        }
	}
});

function getaudit(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="0"){
        html="未审核";
    }else if(cellvalue=="1"){
        html="一级已审核";
    }else if(cellvalue=="2"){
        html="二级已审核";
    }else if(cellvalue=="3"){
        html="三级已审核";
    }
    return html;
}
function ungetaudit(cellvalue, options, rowObject){
    var html="";

    if(cellvalue=="未审核"){
        html="0";
    }else if(cellvalue=="一级已审核"){
        html="1";
    }else if(cellvalue=="二级已审核"){
        html="2";
    }else if(cellvalue=="三级已审核"){
        html="3";
    }
    return html;
}